package com.cunjin.notice.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cunjin.base.service.impl.BaseServiceImpl;
import com.cunjin.notice.dao.NoticeDao;
import com.cunjin.notice.entity.NoticeEntity;
import com.cunjin.notice.service.NoticeService;



@Service
public class NoticeServiceImpl extends BaseServiceImpl<NoticeEntity> implements NoticeService{

	@Autowired
	private NoticeDao noticeDao;

	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> queryByPage(int page, int rows, String searchName) {
		
		//将分页的数据保存在map中
		Map<String, Object> pages=new HashMap<String, Object>();
		
		int before=(page-1)*rows;
		int after=rows;
		pages.put("before", before);
		pages.put("after", after);
		if(null !=searchName){
			pages.put("searchName", searchName+"%");
		}else{
			pages.put("searchName", null);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<NoticeEntity> ls=noticeDao.myqueryByPage(pages);
			map.put("total",noticeDao.findAllByName(pages));
			map.put("rows",ls);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Object> insert(NoticeEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<NoticeEntity> select = noticeDao.select(entity);
			int size = select.size();
			//size==0说明没有
			if(size==0){
				int count = noticeDao.insert(entity);
				if(count==1){
					map.put("code", 200);
					map.put("msg","添加公告成功");
				}
				else{
					map.put("code", 500);
				}
			}else{
				map.put("code", 201);
				map.put("msg","公告已经存在");
			}
		} catch (Exception e) {
			map.put("code", 500);
			e.printStackTrace();
		}
		return map;
	}

	
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> deleteByids(String[] split) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			
			int cs=0;
			for (String s : split) {
				int did = Integer.parseInt(s);
				noticeDao.deleteByPrimaryKey(did);
				cs++;
			}
			map.put("code", 200);
			map.put("count", cs);
		} catch (Exception e) {
			map.put("code",500);
			e.printStackTrace();
		}
		return map;
	}
	
	
}
